//
//  SelectRiskLevelViewController.m
//  MyProject
//
//  Created by Pei on 16/7/9.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//

#import "SelectRiskLevelViewController.h"

@interface SelectRiskLevelViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSArray *_dataArray;
    UITableView *_myTable;
    
    NSIndexPath *_lastIndexPath;
}

@end

@implementation SelectRiskLevelViewController

- (void)viewDidLoad {
    self.navTitle = @"选择";
    [super viewDidLoad];
    
    _lastIndexPath = nil;
    
    NSArray *tempArray = [[NSUserDefaults standardUserDefaults] objectForKey:select_RiskLevel];
    if (tempArray != nil && tempArray.count > 0) {
        _dataArray = tempArray;
    }else{
        [self getData];
    }
    
    [self initTableView];
}

- (void)initTableView
{
    _myTable = [[UITableView alloc] init];
    _myTable.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth, kDeviceHeight-NAV_HEIGHT);
    _myTable.delegate = self;
    _myTable.dataSource = self;
    [self.view addSubview:_myTable];
    
    _myTable.tableFooterView = [UIView new];
}

- (void)getData
{
    [[YMNetLib getInstance] connectWithAction:Dictionary_Type_API method:@"GET" withContent:@"DicType=RiskLevel" withBody:nil withDelegate:self];
}

- (void)NetDoOverSuccess:(NSDictionary*)body
{
    if ([[YMNetDataUtil getSuccessState:body] intValue] == 1)
    {
        _dataArray = [body valueForKey:@"DicList"];
        [[NSUserDefaults standardUserDefaults] setObject:[body valueForKey:@"DicList"] forKey:select_RiskLevel];
        [_myTable reloadData];
    }
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell_house_History";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.textColor = kRGB(205, 206, 207);
    cell.textLabel.font = kAppFont(Home_Small_Size+2);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSDictionary *tempDic = [_dataArray objectAtIndex:indexPath.row];
    
    cell.textLabel.text = [tempDic valueForKey:@"Name"];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54.0f;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSInteger newRow = [indexPath row];
    NSInteger oldRow = -1;
    if (_lastIndexPath != nil)
    {
        oldRow = [_lastIndexPath row];
    }
    
    
    if (newRow != oldRow)
    {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                    indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:_lastIndexPath];
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        
        _lastIndexPath = indexPath;
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *tempDic = [_dataArray objectAtIndex:indexPath.row];
    
    if ([self.delegate respondsToSelector:@selector(passSelectRiskLevel:)])
    {
        [self.delegate passSelectRiskLevel:tempDic];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
